<template>
  <div id="box" class="flex-col">
    <Item :list="typeList" v-model="selectedObjs" @itemClick="itemClick"/>
    <dl class="select flex-row justify-start align-center" style="border-bottom-width: 0px;" v-if="isShowActive">
      <dt>已选条件：</dt>
      <dd v-for="(item,index) in selectedObjs" :key="index">
        {{ item[textKey] }}
      </dd>
    </dl>
  </div>
</template>

<script>
import Item from './item.vue'
export default {
  components: {
    Item
  },
  props: {
    typeList: {},
    isShowActive: {
      type: Boolean,
      default: () => true
    },
    returnObj: {
      type: Boolean,
      default: () => false
    },
    textKey: {
      type: String,
      default: () => 'name'
    },
    valueKey: {
      type: String,
      default: () => 'id'
    },
    value: {}
  },
  data(){
    return {
      selectedObjs: [],
      selectedIds: ''
    }
  },
  watch: {
    selectedObjs: {
      handler(val){
        if(this.returnObj){
          this.$emit('input', val)
        } else {
          const values = val.map(item => item[this.valueKey]).join(',')
          this.selectedIds = values
          this.$emit('input', values)
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    itemClick(item, list){
      this.$emit('itemClick',item, list)
    }
  }
}
</script>

<style scoped lang="scss">
dl.select {
  background: #efefef;
  margin: 0;
  padding: 6px 20px;
}

dl.select dd {
  border: 1px solid #409eff;
  border-radius: 3px;
  background-color: #fff;
  padding: 2px 12px;
  margin-left: 12px;
  font-size: 0.9rem;
  cursor: pointer;
}

dl.select dd span {
  width: 20px;
  height: 20px;
  display: inline-block;
  text-align: center;
  line-height: 20px;
  background: #c4284d;
  color: #fff;
  margin-left: 10px;
  cursor: pointer;
  border-radius: 5px;
}
</style>



